package Core_no_tiles.playerTest;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import Core.Core;
import Engine.InputHandling.Keyboard;
import Engine.InputHandling.Mouse;
import Engine.Utils.Vector2f;

public class Player extends Entity {

	protected BufferedImage[] images;
    protected boolean animating;
    protected int activeImage = 0;
    protected int counter = 0;
    protected int frameTime = 30;
	
	public Player(Vector2f pos, BufferedImage[] images, boolean solid) {
		super(pos, solid);
		this.images = images;
		animating = true;
	}
	
	public Player(Vector2f pos, BufferedImage image, boolean solid) {
		super(pos, solid);
		this.image = image;
		animating = false;
	}

	@Override
    public void tick() {
        if (animating) animate();
        move();
        moveToMouse();
    }
    
    @Override
    public void render(Graphics g) {
        g.drawImage(image, (int) (pos.getX() - Core.getMapPos().getX()), (int) (pos.getY() - Core.getMapPos().getY()), null);
    }
    
    public void move() {
    	if (Keyboard.keysDown[KeyEvent.VK_D]) setPos(getPos().addX(5.0f));
		if (Keyboard.keysDown[KeyEvent.VK_A]) setPos(getPos().addX(-5.0f));
		if (Keyboard.keysDown[KeyEvent.VK_W]) setPos(getPos().addY(-5.0f));
		if (Keyboard.keysDown[KeyEvent.VK_S]) setPos(getPos().addY(5.0f));
    }
    
    public void moveToMouse() {
    	if (Mouse.mouseClicked(MouseEvent.BUTTON1)) {
    		System.out.println(Mouse.getPos().toString());
    	}
    }
    
    public void animate() {
        if (counter > frameTime) {
            if (activeImage < images.length - 1) {
                activeImage++;
                image = images[activeImage];
            } else {
                activeImage = 0;
                image = images[activeImage];
            }
            counter = 0;
        }
        counter++;
    }
	
}
